package com.kingdee.pda.commonmethod;

import android.content.Context;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.kingdee.pda.Api;
import com.kingdee.pda.entity.WarehouseLocation;
import com.kingdee.pda.util.WarehouseUtil;

import java.util.HashMap;

/**
 * @author：dlj
 * @data：2022/1/14
 * @des：库位、sku校验、库存查询 Presenter（改造原来代码得到的，可能有点乱）
 */
public class QueryStockPresenter {

    private Context context;
    private QueryStockView view;

    public QueryStockPresenter(Context context, QueryStockView queryStockView) {
        this.context = context;
        this.view = queryStockView;
    }

    //校验库位
    public void queryWarehouseLocation(final long warehouseId, String locationCode) {
        HashMap<String, Object> json = new HashMap<>();
        json.put("warehouseId", warehouseId);
        json.put("locationCode", locationCode);
        MyObserver<WarehouseLocation> myObserver = new MyObserver<WarehouseLocation>(context) {
            @Override
            public void onSuccess(BaseResponse<WarehouseLocation> result) {
                //适应原来的数据结构吧
                WarehouseLocation data = result.data;
                data.setLocationId(data.getId());
                view.queryWarehouseLocationSuccess(data);
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                view.queryWarehouseLocationFailed(msg);
            }
        };
        HttpUtils.request(Api.class)
                .queryWarehouseLocation(ConstantUtil.PDA_query_location, json)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(myObserver);
    }

    //查询sku的在具体库位的库存信息
    public void queryGoodsLocationStock(final WarehouseLocation moveLocation, long skuId, int isDefaultLocation) {
        HashMap<String, Object> json = new HashMap<>();
        json.put("warehouseId", moveLocation.getWarehouseId() == 0 ? WarehouseUtil.getCurrentWarehouse().getId() : moveLocation.getWarehouseId());
        json.put("locationId", moveLocation.getLocationId());
        json.put("skuId", skuId);
        json.put("defaultLocation", isDefaultLocation);
        MyObserver<WarehouseLocation> myObserver = new MyObserver<WarehouseLocation>(context) {
            @Override
            public void onSuccess(BaseResponse<WarehouseLocation> result) {
                view.queryGoodsLocationStockSuccess(result.data);
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                view.queryGoodsLocationStockFailed(msg);
            }
        };
        HttpUtils.request(Api.class)
                .queryGoodsLocationStock(ConstantUtil.PDA_query_sku_stock_inlocation, json)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(myObserver);

    }
}
